package cn.iocoder.yudao.module.indicator.service.index;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.indicator.domain.entity.ElementData;
import cn.iocoder.yudao.module.indicator.domain.entity.IndexElementContact;
import cn.iocoder.yudao.module.indicator.domain.entity.Indicator;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.indicator.dao.index.IndexElementContactDao;
import cn.iocoder.yudao.module.indicator.dao.index.IndicatorDao;
import cn.iocoder.yudao.module.indicator.domain.dto.IndicatorDto;
import cn.iocoder.yudao.module.indicator.domain.vo.IndicatorVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ZYW
 * @description: TODO
 * @date 2023-12-26 15:53
 */
@Service
public class IndicatorServiceImpl implements IndicatorService {
    @Autowired
    IndicatorDao indicatorDao;
    @Autowired
    IndexElementContactDao indexElementContactDao;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertIndicator(Indicator indicator) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        String[] elementIds = indicator.getElementIds().split("-");
        if (elementIds.length == 0) {
            logger.error("No element IDs provided for insertion.");
            throw new IllegalArgumentException("No element IDs provided for insertion.");
        }
        indicatorDao.insert(indicator);
        //新增元素和指标关联

        insertContact(indicator.getElementIds().split("-"), String.valueOf(indicator.getId()));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateIndicator(Indicator indicator) {
        //IndicatorVo indicatorVo = BeanUtils.toBean(indicatorDto, IndicatorVo.class);
        String indexId = String.valueOf(indicator.getId());
        //删联
        indexElementContactDao.delete("INDEX_ID",indexId );
        String elementIds = indicator.getElementIds();
        if (StringUtils.isNoneBlank(elementIds)){
            //新增元素和指标关联
            insertContact(elementIds.split("-"),indexId);
        }
        return indicatorDao.updateById(indicator);
    }

    protected void insertContact(String[] elementIds,String indexId){
        List<IndexElementContact> list = Arrays.stream(elementIds)
                .map(elementId -> {
                    IndexElementContact indexElementContact = new IndexElementContact();
                    indexElementContact.setElementId(elementId);
                    indexElementContact.setIndexId(indexId);
                    return indexElementContact;
                })
                .collect(Collectors.toList());
        indexElementContactDao.insertBatch(list);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteIndicator(String id) {
        //删联
        indexElementContactDao.delete("INDEX_ID",id);
        return indicatorDao.deleteById(id);
    }

    @Override
    public PageResult<Indicator> queryIndicator(IndicatorDto indicatorDto) {
        return indicatorDao.selectPage(indicatorDto);
    }
    @Override
    public IndicatorVo queryIndicatorById(String id) {
        Indicator indicator = indicatorDao.selectOne("id", id);
        String elementIds = indicator.getElementIds();
        if (StringUtils.isNoneBlank(elementIds)) {
            //放入元素名称
        List<ElementData> elementDataList = indicatorDao.getElementDataByIds(elementIds);
        if (CollectionUtil.isNotEmpty(elementDataList)){
            Map<Long, List<ElementData>> elementDataMap = elementDataList.stream().collect(Collectors.groupingBy(ElementData::getId));
            indicator.setElementInfoMap(elementDataMap);
        }
    }
        return BeanUtils.toBean(indicator, IndicatorVo.class);
    }
}
